/* 
 * Cashbox is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "CBAccountsPreferenceViewController.h"
#import "CBAccount.h"
#import <Cashbox/CBDataControllerProtocol.h>
#import <Cashbox/CBAccountControllerProtocol.h>

@implementation CBAccountsPreferenceViewController

+ (void)registerDefaults {
}

+ (NSString *)nibName {
	return @"AccountsPreferences";
}

- (NSString *)label {
	return @"Accounts";
}

- (NSImage *)image {
	return [NSImage imageNamed:@"accounts_pref"];
}

- (void)awakeFromNib {
	
	[removeButton bind:@"enabled" toObject:accountsController withKeyPath:@"canRemove" options:nil];
	
	[accountsController setManagedObjectContext:[[cashbox dataController] managedObjectContext]];
	[accountsController setEntityName:[CBAccount entityName]];
	[accountsController setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease]]];
	[accountsController fetchWithRequest:nil merge:NO error:NULL];

	[addAccountTypesController addObjects:[[cashbox accountController] accountTypes]];
	
	[accountTable setIntercellSpacing:NSMakeSize(0, 0)];
	[[[accountTable tableColumns] objectAtIndex:0] setDataCell:[[[AIImageTextCell alloc] init] autorelease]];
}

- (IBAction)addAccount:(id)sender {
	[NSApp beginSheet:addAccountPanel
	   modalForWindow:[[self view] window]
		modalDelegate:nil
	   didEndSelector:nil
		  contextInfo:nil];
}

- (IBAction)finishAddingAccount:(id)sender {
	
	// kill the sheet first
	[addAccountPanel orderOut:nil];
	[NSApp endSheet:addAccountPanel];
	
	Class accountType = [[addAccountTypesController selectedObjects] firstObject];
	NSString *name = [addAccountNameTextField stringValue];
	if (accountType && name && ![name isEqualToString:@""]) {
		CBAccount *a = [[[accountType alloc] init] autorelease];
		[a setName:name];
		[[[cashbox dataController] managedObjectContext] processPendingChanges];
		[accountsController setSelectedObjects:[NSArray arrayWithObject:a]];
	} else if (!accountType) {
		NSBeginAlertSheet(FSLocalizedString(@"Failed to create account", nil),
						  FSLocalizedString(@"OK", nil),
						  nil,
						  nil,
						  [[self view] window],
						  nil,
						  nil,
						  nil,
						  nil,
						  FSLocalizedString(@"You must specify a type in order to create an account.", nil));
	} else if (!name || [name isEqualToString:@""]) {
		NSBeginAlertSheet(FSLocalizedString(@"Failed to create account", nil),
						  FSLocalizedString(@"OK", nil),
						  nil,
						  nil,
						  [[self view] window],
						  nil,
						  nil,
						  nil,
						  nil,
						  FSLocalizedString(@"You must specify a name in order to create an account.", nil));
	}	
}

- (IBAction)cancelAddingAccount:(id)sender {
	[addAccountPanel orderOut:nil];
	[NSApp endSheet:addAccountPanel];	
}

- (IBAction)removeAccount:(id)sender {
	CBAccount *a = [[accountsController selectedObjects] firstObject];
	if (a) {
		NSBeginAlertSheet([NSString stringWithFormat:FSLocalizedString(@"Delete the account %@?", nil), [a name]],
						  FSLocalizedString(@"Delete", nil),
						  FSLocalizedString(@"Cancel", nil),
						  nil,
						  [[self view] window],
						  self,
						  @selector(removeAccountDidEnd:returnCode:contextInfo:),
						  nil,
						  a,
						  [NSString stringWithFormat:FSLocalizedString(@"Deleting an account will permanently remove all of its transactions as well.  Are you sure you want to delete the acount %@?", nil), [a name]]);		
	}
}

- (void)removeAccountDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void  *)contextInfo {
	CBAccount *a = contextInfo;
	if (returnCode == NSAlertDefaultReturn) {
		// delete the account
		[[[cashbox dataController] managedObjectContext] deleteObject:a];
	}
}

- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex {
	return NO;
}

- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(int)row {
	[(AIImageTextCell *)cell setSubString:[[(CBAccount *)[[accountsController arrangedObjects] objectAtIndex:row] class] name]];
}

@end
